package sc;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SendMessage extends JDialog implements ActionListener
{
    private static final long serialVersionUID = 1L;
    public static Image icon = Images.getImage("/images/sendmessageicon.png");
    private static Image bckg = Images.getImage("/images/sendmessagebg.jpg");
    private static SendMessage instance = null;
    private static JLabel receiver = new JLabel();
    private static String from;
    private static String to;
    private static FancyTextArea subject;
    private static FancyTextArea message;
    
    public class ImagePanel extends JPanel
    {
        private static final long serialVersionUID = 1L;

        @Override
        protected void paintComponent(Graphics g)
        {
            if (SendMessage.bckg != null)
                g.drawImage(SendMessage.bckg, 0, 0, this);
        }
    }


    private SendMessage()
    {	
        setIconImage(icon);
        setTitle("Send Message");
        setResizable(false);

        // Natural width, maximum height
        GridBagConstraints gbc = new GridBagConstraints();
		gbc.fill = GridBagConstraints.VERTICAL;		
        
        JPanel panel = new ImagePanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        Dimension labelSize = new Dimension(60, 20);
        subject = new FancyTextArea(31, 1, 30);
        
        JPanel subpanel = new JPanel(new GridBagLayout());
        subpanel.setOpaque(false);
        JLabel label = new JLabel("To:");
        label.setPreferredSize(labelSize);
        label.setHorizontalAlignment(JLabel.RIGHT);
        receiver.setMinimumSize(subject.getPreferredSize());
        receiver.setPreferredSize(subject.getPreferredSize());
        label.setHorizontalAlignment(JLabel.RIGHT);
        subpanel.add(label);
        subpanel.add(Box.createRigidArea(new Dimension(5,5)));
        subpanel.add(receiver);
        subpanel.add(Box.createRigidArea(new Dimension(5,5)));
        panel.add(subpanel);

        subpanel = new JPanel(new GridBagLayout());
        subpanel.setOpaque(false);
        label = new JLabel("Subject:");
        label.setPreferredSize(labelSize);
        label.setHorizontalAlignment(JLabel.RIGHT);
        label.setOpaque(false);
        subpanel.add(label, gbc);
        subpanel.add(Box.createRigidArea(new Dimension(5,5)));
        subject.setMinimumSize(subject.getPreferredSize());
        subpanel.add(subject.getScrollPane(), gbc);
        subpanel.add(Box.createRigidArea(new Dimension(5,5)));
        panel.add(subpanel);

        panel.add(Box.createRigidArea(new Dimension(5,5)));

        subpanel = new JPanel(new GridBagLayout());
        subpanel.setOpaque(false);
        label = new JLabel("Message:");
        label.setPreferredSize(labelSize);
        label.setMaximumSize(labelSize);
        label.setHorizontalAlignment(JLabel.RIGHT);
        label.setVerticalAlignment(JLabel.TOP);
        label.setOpaque(false);
        subpanel.add(label, gbc);
        subpanel.add(Box.createRigidArea(new Dimension(5,5)));
        message = new FancyTextArea(31, 10, 500);
        message.setMinimumSize(message.getPreferredSize());
        subpanel.add(message.getScrollPane(), gbc);
        subpanel.add(Box.createRigidArea(new Dimension(5,5)));
        panel.add(subpanel);

        subpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        subpanel.setOpaque(false);
        JButton button = new JButton(" Send ");
        button.addActionListener(this);
        subpanel.add(button);
        button = new JButton("Cancel");
        button.addActionListener(this);
        subpanel.add(button);
        panel.add(subpanel);

        // Add the root Component to the content pane and call pack to perform layout
        getContentPane().add(panel);
        pack();
    }

    public static void open(String from, String to)
    {
        if (SendMessage.instance == null)
            SendMessage.instance = new SendMessage();

        SendMessage.receiver.setText(Member.getMemberByID(to).getName());
        SendMessage.from = from;
        SendMessage.to = to;
        SendMessage.subject.setText("");
        SendMessage.message.setText("");
        SendMessage.instance.setVisible(true);
        SendMessage.instance.setAlwaysOnTop(true);
        
    }

    public void actionPerformed(ActionEvent e)
    {
        String cmd = e.getActionCommand();
        if (cmd == "Cancel")
            setVisible(false);
        else if (cmd == " Send ")
        {
            OpenURL.postPM(SendMessage.from, SendMessage.to, SendMessage.subject.getText(), SendMessage.message.getText());
            setVisible(false);
        }
    }
}
